package com.joker.aigc.oj.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joker.aigc.oj.domain.dto.problemsubmit.ProblemSubmitAddDTO;
import com.joker.aigc.oj.domain.dto.problemsubmit.ProblemSubmitQueryRequest;
import com.joker.aigc.oj.domain.entity.ProblemSubmit;
import com.joker.aigc.domain.entity.R;
import com.joker.aigc.domain.entity.User;
import com.joker.aigc.oj.domain.vo.problemsubmit.ProblemSubmitVO;
import com.joker.aigc.oj.domain.vo.problemsubmit.SubmitSummaryVo;
import com.joker.aigc.oj.service.ProblemSubmitService;
import com.joker.aigc.service.UserService;
import com.joker.aigc.utils.AssertUtil;
import com.joker.aigc.utils.RequestHolder;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author joker
 * @date 2024/08/04 17:07
 * @description 题目提交接口
 */
@RestController
@RequestMapping("/oj/problem_submit")
@Slf4j
@Validated
public class ProblemSubmitController {


    @Resource
    private ProblemSubmitService problemSubmitService;
    @Resource
    private UserService userService;


    /**
     * 查看一个人的所有提交记录
     * @return
     */
    @GetMapping("/summary")
    public R<SubmitSummaryVo> getSubmitSummary() {
        Long uid = RequestHolder.get().getUid();
        SubmitSummaryVo vo = problemSubmitService.getSubmitSummary(uid);
        return R.ok(vo);
    }

    /**
     * 用户 提交题目
     * @param problemSubmitAddDTO 提交信息
     * @return 提交记录的 id
     */
    @PostMapping
    public R<ProblemSubmit> doProblemSubmit(@RequestBody @NotNull @Valid ProblemSubmitAddDTO problemSubmitAddDTO) {
        Long uid = RequestHolder.get().getUid();
        User byId = userService.getById(uid);
        ProblemSubmit submitResult = problemSubmitService.doProblemSubmit(problemSubmitAddDTO, byId);
        return R.ok(submitResult);
    }

    /**
     * 分页获取题目提交历史
     * @param problemSubmitQueryRequest 查询参数
     * @return 题目提交历史信息
     */
    @PostMapping("/page/vo")
    public R<Page<ProblemSubmitVO>> listProblemSubmitVoByPage(@RequestBody ProblemSubmitQueryRequest problemSubmitQueryRequest) {
        // 返回脱敏信息
        return R.ok(problemSubmitService.listProblemSubmitVoByPage(problemSubmitQueryRequest));
    }

    /**
     * 分页获取题目提交历史
     * @param problemSubmitQueryRequest 查询参数
     * @return 题目提交历史信息
     */
    @PostMapping("/pageByUser/vo")
    public R<Page<ProblemSubmitVO>> listProblemSubmitVoByUserId(@RequestBody ProblemSubmitQueryRequest problemSubmitQueryRequest) {
        Long uid = RequestHolder.get().getUid();
        AssertUtil.isNotEmpty(uid, "用户id不能为空");
        problemSubmitQueryRequest.setUid(uid);
        // 返回脱敏信息
        return R.ok(problemSubmitService.listProblemSubmitVoByUserId(problemSubmitQueryRequest));
    }


    /**
     * 根据问题提交id 获取
     * @param problemSubmitId  问题提交记录
     * @return 提交信息
     */
    @GetMapping("/{id}")
    public R<ProblemSubmit> getProblemSubmitVoById(@PathVariable("id") Long problemSubmitId) {
        // 返回脱敏信息
        return R.ok(problemSubmitService.getById(problemSubmitId));
    }
}
